package com.kgc.community.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.community.pojo.Equipment;
import com.kgc.community.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("device")
@Controller
public class EquipmentController {

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    EquipmentService equipmentService;

    @RequestMapping("showEditPage.html")
    public String showEditPage(ModelMap modelMap,Integer id){
        String url = "updateEquipment.action";

        Equipment equipment = equipmentService.searchById(id);
        if (id == null){
            url = "insertEquipment.action";
        }
        modelMap.addAttribute("equipment",equipment).addAttribute("url",url);

        return "device_add";
    }

    @RequestMapping("searchEquipmentPage.html")
    public String searchEquipmentPage(ModelMap modelMap
            , @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum
            , @RequestParam(value = "pageSize",defaultValue = "8") Integer pageSize
            , String condition
            , String startTime
            , String endTime){

        PageInfo<Equipment> pageInfo = equipmentService.searchEquipmentToPage(condition,pageNum, pageSize,startTime,endTime);
        modelMap.addAttribute("pageInfo",pageInfo).addAttribute("condition",condition).addAttribute("startTime",startTime).addAttribute("endTime",endTime);

        return "device_list";
    }

    @ResponseBody
    @RequestMapping("insertEquipment.action")
    public Map<String,Object> insertEquipment(Equipment equipment,String expectTimeStr,String buyTimeStr){
        Map<String,Object> map = new HashMap<>();

        if (expectTimeStr!=null && expectTimeStr.length()>0){
            try {
                Date date = format.parse(expectTimeStr);
                equipment.setExpectTime(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (buyTimeStr!=null && buyTimeStr.length()>0){
            try {
                Date date = format.parse(buyTimeStr);
                equipment.setBuyTime(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Integer res = equipmentService.insertSelective(equipment);

        if (res>0){
            map.put("status",1);
            map.put("info","添加成功！");
        }else{
            map.put("status",0);
            map.put("info","添加失败！");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping("updateEquipment.action")
    public Map<String,Object> updateEquipment(Equipment equipment,String expectTimeStr,String buyTimeStr){
        Map<String,Object> map = new HashMap<>();

        if (expectTimeStr!=null && expectTimeStr.length()>0){
            try {
                Date date = format.parse(expectTimeStr);
                equipment.setExpectTime(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (buyTimeStr!=null && buyTimeStr.length()>0){
            try {
                Date date = format.parse(buyTimeStr);
                equipment.setBuyTime(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Integer res = equipmentService.updateById(equipment);
        if (res>0){
            map.put("status",1);
            map.put("info","修改成功！");
        }else{
            map.put("status",0);
            map.put("info","修改失败！");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping("deleteEquipment.action")
    public Map<String,Object> deleteEquipment(@RequestParam(value = "id") String idStr){
        Map<String,Object> map = new HashMap<>();

        List<Integer> idList = new ArrayList<>();

        String[] arrays = idStr.split(",");
        if (arrays.length>0){
            for (String array : arrays) {
                idList.add(Integer.parseInt(array));
            }
        }

        Integer res = equipmentService.deleteByIdList(idList);
        if (res>0){
            map.put("status",1);
            map.put("info","删除成功！");
        }else{
            map.put("status",0);
            map.put("info","删除失败！");
        }

        return map;
    }
}
